package com.fstart.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fstart.common.Result;
import com.fstart.sys.entity.dto.PunchinDto;
import com.fstart.sys.entity.Punchin;
import com.fstart.sys.entity.PunchinPhoto;
import com.fstart.sys.service.IPunchinPhotoService;
import com.fstart.sys.service.IPunchinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xibing
 * @since 2023-05-05
 */
@RestController
@RequestMapping("/sys/punchin")
public class PunchinController {
    @Autowired
    private IPunchinService punchinService;

    @Autowired
    private IPunchinPhotoService punchinPhotoService;

    /**
     * 获取所有的打卡信息
     * @return
     */
    @GetMapping()
    public Result<List<PunchinDto>> getAllPunchin(){

        List<PunchinDto> data = punchinService.getAll();

        // 排序
        Collections.sort(data, new Comparator<PunchinDto>() {
            @Override
            public int compare(PunchinDto o1, PunchinDto o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });

        return Result.success(data);
    }

    @PostMapping()
    public Result<String> save(@RequestBody PunchinDto punchinDto){

        Punchin punchin = new Punchin();

        // 设置打卡数据
        punchin.setId(UUID.randomUUID().toString());
        punchin.setPunchin(punchinDto.getPunchin());
        punchin.setCreateTime(LocalDateTime.now());
        punchin.setUpdateTime(LocalDateTime.now());
        punchin.setUserId(punchinDto.getUserId());

        boolean save = punchinService.save(punchin);

        // 保存图片连接
        if(punchinDto.getImageUrls() != null){
            for (String url : punchinDto.getImageUrls()) {
                PunchinPhoto photo = new PunchinPhoto();
                photo.setPunchinId(punchin.getId());
                photo.setPhotoAddr(url);
                photo.setCreateTime(LocalDateTime.now());

                punchinPhotoService.save(photo);
            }
        }


        if(save){
            return Result.success("发布成功");
        }else{
            return Result.fail("发布失败");
        }

    }

    /**
     * 更具id删除说说
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable String id){
        boolean b = punchinService.removeById(id);

        // 删除图片数据
        LambdaQueryWrapper<PunchinPhoto> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PunchinPhoto::getPunchinId, id);
        punchinPhotoService.remove(wrapper);

        if(b){
            return Result.success("删除成功");
        }else{
            return Result.fail("删除失败，请重试。");
        }
    }

    // 每次查询五条
    @GetMapping("/page")
    public Result<Map<String, Object>> getPage(@RequestParam(value = "pageNo") Integer pageNo,
                                               @RequestParam(value= "pageSize") Integer pageSize){
        PunchinDto punchinDto = new PunchinDto();
        punchinDto.setStartPage((pageNo - 1) * pageSize);
        punchinDto.setPageSize(pageSize);

        Map<String, Object> data = punchinService.getByPage(punchinDto);


        List<Punchin> punchins = (List<Punchin>)data.get("punchins");
        List<PunchinPhoto> photos = new ArrayList<>();


        for (Punchin punchin : punchins) {
            LambdaQueryWrapper<PunchinPhoto> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PunchinPhoto::getPunchinId, punchin.getId());
            List<PunchinPhoto> list = punchinPhotoService.list(wrapper);
            for (PunchinPhoto punchinPhoto : list) {
                photos.add(punchinPhoto);
            }
        }

        data.put("PhotoUrls", photos);

        return Result.success(data, "查询成功");
    }

}
